package uit.nfcmenu.waiter.activities;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import uit.nfcmenu.models.DatabaseManager;
import uit.nfcmenu.models.DbDefines;
import uit.nfcmenu.models.IDataChangeHandler;
import uit.nfcmenu.models.ItemModel;
import uit.nfcmenu.models.OrderModel;
import uit.nfcmenu.utils.CommConstant;
import uit.nfcmenu.utils.ImageDownloader;
import uit.nfcmenu.waiter.R;

public class DetailActivity extends Activity implements OnClickListener, IDataChangeHandler {

	private TextView tvQuantity, tvTable, tvDiscount, tvPrice, tvComment, tvDescriptionContent;
	private Button bntOrderList, bntLogout, bntMennu, bntOrder;
	private ImageView img;
	private ViewFlipper vf;
	private float oldTouchValue;
	
	DatabaseManager mDatabaseManager;
	OrderModel mOrder;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detail);
		initView();
		initData();		
		registerEventListener();
	}

	private void initData() {
		mDatabaseManager = DatabaseManager.getInstance(this);
		
		mOrder = new OrderModel();
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
//			ItemModel item = (ItemModel)bundle.get("item");
			int value = bundle.getInt("itemId");
			String url = bundle.getString("itemUrl");
			mOrder.setItemID(value);
			mOrder.setDescription(bundle.getString("description"));
			ImageDownloader ima = new ImageDownloader();
			ima.download(url, img);
//			tvDescriptionContent.setText(mOrder.getDescription());
		}
	}
	
	private void initView() {
		// TODO Auto-generated method stub
		tvQuantity = (TextView) findViewById(R.id.txtQuantity);
		tvTable = (TextView) findViewById(R.id.txtTable);
		tvDiscount = (TextView) findViewById(R.id.lblDiscount);
		tvPrice = (TextView) findViewById(R.id.lblPrice);
		tvComment = (EditText) findViewById(R.id.txtNote);
		tvDescriptionContent = (TextView) findViewById(R.id.lblDescriptionContent);
		
		bntOrderList = (Button) findViewById(R.id.btnOrderList);
		bntLogout = (Button) findViewById(R.id.btnLogout);
		bntMennu = (Button) findViewById(R.id.btnMenu);
		bntOrder = (Button) findViewById(R.id.bntOrderFinal);
		
		img = (ImageView) findViewById(R.id.img_goods);
		vf = (ViewFlipper) findViewById(R.id.flipperDetail);
	}

	private void registerEventListener() {
		bntOrderList.setOnClickListener(this);
		bntLogout.setOnClickListener(this);
		bntMennu.setOnClickListener(this);
		bntOrder.setOnClickListener(this);
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnOrderList:
			launchOrderListActivity();
			break;
		case R.id.btnLogout:
			launchLogInActivity();
			break;
		case R.id.btnMenu:
			launchMainMenuActivity();
			break;
		case R.id.bntOrderFinal:
			requestToOrder();
			break;
		default:
			// TODO not do ??
			getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		}
	}

	private void requestToOrder() {
		String quantity = tvQuantity.getText().toString();
		String table = tvTable.getText().toString();
		if ("".equals(quantity) || "".equals(table)) {
			new AlertDialog.Builder(DetailActivity.this)
					.setTitle("Message")
					.setMessage("Please input quantity number and table index")
					.setNegativeButton("OK", null)
					.create()
					.show();
		}
		else {
			mOrder.setStatus(CommConstant.Status[0]); // new 
			mOrder.setQuantity(Integer.parseInt(quantity));
			mOrder.setTableNo(Integer.parseInt(table));
			mOrder.setComment(tvComment.getText().toString());
			if (mDatabaseManager.postOrder(mOrder)) {
				//launchOrderListActivity();
				finish();
			}
			else {
				// clears the fields
			}
		}
	}

	private void launchMainMenuActivity() {
		Intent intent = new Intent(this, MainMenuActivity.class);
		startActivity(intent);
	}

	private void launchLogInActivity() {
		Intent intent = new Intent(this, LoginActivity.class);
		startActivity(intent);
	}

	private void launchOrderListActivity() {
		Intent intent = new Intent(this, OrderListActivity.class);
		startActivity(intent);
	}

	public void onDataChanged() {
		// TODO Auto-generated method stub
		
	}

	public Context getContext() {
		// TODO Auto-generated method stub
		return getBaseContext();
	}
	
	@Override
    public boolean onTouchEvent(MotionEvent touchevent) {
        switch (touchevent.getAction())
        {
            case MotionEvent.ACTION_DOWN:
            {
                oldTouchValue = touchevent.getX();
                break;
            }
            case MotionEvent.ACTION_UP:
            {
                //if(this.searchOk==false) return false;
                float currentX = touchevent.getX();
                if (oldTouchValue < currentX)
                {
                	             	
                    vf.setInAnimation(AnimationHelper.inFromLeftAnimation());
                    vf.setOutAnimation(AnimationHelper.outToRightAnimation());
                    
                                    
                    vf.showNext();
//                    if(radio1.isChecked()==true){
//                    	radio1.setChecked(false);
//                    	radio2.setChecked(true);
//                    }
//                    else if(radio2.isChecked()==true){
//                    	radio2.setChecked(false);
//                    	radio3.setChecked(true);
//                    }
//                    else if(radio3.isChecked()==true){
//                    	radio3.setChecked(false);
//                    	radio1.setChecked(true);
//                    }
                }
                if (oldTouchValue > currentX)
                {
                    vf.setInAnimation(AnimationHelper.inFromRightAnimation());
                    vf.setOutAnimation(AnimationHelper.outToLeftAnimation());
                    vf.showPrevious();
//                    if(radio1.isChecked()==true){
//                    	radio1.setChecked(false);
//                    	radio3.setChecked(true);
//                    }
//                    else if(radio2.isChecked()==true){
//                    	radio2.setChecked(false);
//                    	radio1.setChecked(true);
//                    }
//                    else if(radio3.isChecked()==true){
//                    	radio3.setChecked(false);
//                    	radio2.setChecked(true);
//                    }
                }
            break;
            }
        }
        return false;
    }
}
